package com.egene.tw.activity.settings;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.activity.edit.EditTextActivity;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.dao.UserDao;
import com.egene.tw.data.RealmHelper;
import com.egene.tw.data.entity.DeviceSetting;
import com.egene.tw.model.User;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@EActivity(R.layout.activity_account)
public class AccountActivity extends BaseActivity {
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;


    @ViewById
    TextView tvName;
    @ViewById
    TextView tvSex;
    @ViewById
    TextView tvAge;
    @ViewById
    TextView tvWeight;
    @ViewById
    TextView tvHeight;

    @ViewById
    ImageView imgWear;
    @ViewById
    ImageView imgHeight;
    @ViewById
    ImageView imgWeight;
    @ViewById
    ImageView imgJuli;

    @ViewById
    TextView textBloodHeight;
    @ViewById
    TextView textBloodLow;
    @ViewById
    TextView textStep;

    @Bean
    UserDao userDao;

    DeviceSetting deviceSetting;
    User self;

    @AfterViews
    void initViews() {
        initToolBar();
        self = userDao.getUser();
        tvName.setText(self.getUserName());
        if (self.getSex().equals("" + User.FEMALE)) {
            tvSex.setText(R.string.female);
        } else if (self.getSex().equals("" + User.MALE)) {
            tvSex.setText(R.string.male);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDay = null;
        try {
            birthDay = simpleDateFormat.parse(self.getBirthdayString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (birthDay != null) {
            Date today = new Date(System.currentTimeMillis());
            int y1 = today.getYear();
            int y2 = birthDay.getYear();

            int age = y1 - y2;
            tvAge.setText(age > 0 ? "" + age : "0");
        }
        tvWeight.setText(String.format("%s kg", self.getWeight()));
        tvHeight.setText(String.format("%s cm", self.getHeight()));

        loadSetting();
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.personal_detail);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Click(R.id.imgWear)
    public void imgWear() {
        if (deviceSetting == null) {
            return;
        }
        deviceSetting.setWear(!deviceSetting.isWear());
        RealmHelper.saveSettings(deviceSetting);
        loadSetting();
    }

    @Click(R.id.imgHeight)
    public void imgHeight() {
        if (deviceSetting == null) {
            return;
        }
        deviceSetting.setHeight(!deviceSetting.isHeight());
        RealmHelper.saveSettings(deviceSetting);
        loadSetting();
    }

    @Click(R.id.imgWeight)
    public void imgWeight() {
        if (deviceSetting == null) {
            return;
        }
        deviceSetting.setWeight(!deviceSetting.isWeight());
        RealmHelper.saveSettings(deviceSetting);
        loadSetting();
    }

    @Click(R.id.imgJuli)
    public void imgJuli() {
        if (deviceSetting == null) {
            return;
        }
        deviceSetting.setJuli(!deviceSetting.isJuli());
        RealmHelper.saveSettings(deviceSetting);
        loadSetting();
    }


    @Click(R.id.viewBloodHeight)
    public void viewBloodHeight() {
        Intent intent = EditTextActivity.generate(this, getString(R.string.shousouya), deviceSetting.getBloodHeight() + "");
        startActivityForResult(intent, 100);
    }

    @Click(R.id.viewBloodLow)
    public void viewBloodLow() {
        Intent intent = EditTextActivity.generate(this, getString(R.string.shuzhangya), deviceSetting.getBloodLow() + "");
        startActivityForResult(intent, 101);
    }

    @Click(R.id.viewStep)
    public void viewStep() {
        Intent intent = EditTextActivity.generate(this, getString(R.string.target_step), deviceSetting.getTargetStep() + "");
        startActivityForResult(intent, 102);
    }


    private void loadSetting() {
        deviceSetting = RealmHelper.loadSettings();
        imgWear.setImageResource(deviceSetting.isWear() ? R.drawable.my_head_1 : R.drawable.my_head_2);
        imgJuli.setImageResource(deviceSetting.isJuli() ? R.drawable.my_juli_1 : R.drawable.my_juli_2);
        imgHeight.setImageResource(deviceSetting.isHeight() ? R.drawable.my_height_1 : R.drawable.my_height_2);
        imgWeight.setImageResource(deviceSetting.isWeight() ? R.drawable.my_weight_1 : R.drawable.my_weight_2);

        textBloodHeight.setText(deviceSetting.getBloodHeight() + " mmHg");
        textBloodLow.setText(deviceSetting.getBloodLow() + " mmHg");
        textStep.setText(deviceSetting.getTargetStep() + " 步");

        if (self != null) {
            int weight = 0;
            try {
                weight = Integer.parseInt(self.getWeight());
            } catch (NumberFormatException e) {
            }
            if (deviceSetting.isWeight()) {
                tvWeight.setText(Math.round(weight * 2.2046226) + "磅");
            } else {
                tvWeight.setText(weight + "千克");
            }


            int height = 0;
            try {
                height = Integer.parseInt(self.getHeight());
            } catch (NumberFormatException e) {
            }
            if (deviceSetting.isHeight()) {
                tvHeight.setText(Math.round(height * 0.0328084) + "英呎");
            } else {
                tvHeight.setText(height + "釐米");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (deviceSetting == null) {
            return;
        }
        if (data == null) {
            return;
        }
        String text = data.getStringExtra("text");
        int value = 0;
        try {
            value = Integer.parseInt(text);
        } catch (NumberFormatException e) {
        }
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    deviceSetting.setBloodHeight(value);
                    RealmHelper.saveSettings(deviceSetting);
                    loadSetting();
                    break;
                case 101:
                    deviceSetting.setBloodLow(value);
                    RealmHelper.saveSettings(deviceSetting);
                    loadSetting();
                    break;
                case 102:
                    deviceSetting.setTargetStep(value);
                    RealmHelper.saveSettings(deviceSetting);
                    loadSetting();
                    break;
            }
        }
    }
}
